/**    
* @Title: IndexController.java  
* @Package com.kaver.modular.api   
* @author roc   
* @date 2019年1月31日 下午3:39:56  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.modular.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.kaver.core.common.constant.state.ChangeType;
import com.kaver.modular.dc.service.IDcAppService;
import com.kaver.modular.dc.service.IDcConfigVersionService;
import com.kaver.modular.dc.service.IDcEnvService;
import com.kaver.modular.system.model.DcConfigVersion;
import com.kaver.modular.system.transfer.ConfigItem;
import com.kaver.modular.system.transfer.FetchDto;
import com.kaver.modular.system.transfer.PullDto;

/**  
* @ClassName: IndexController  
* @Description: 配置拉取 
* @author roc  
* @date 2019年1月31日 下午3:39:56  
*    
*/
@Controller
public class IndexController {
	@Autowired
	private IDcConfigVersionService dcConfigVersionService;
    @Autowired
    private IDcAppService dcAppService;
    @Autowired
    private IDcEnvService dcEnvService;
    @Autowired
	private RedisTemplate<String, Object>  redisTemplate;
	/**
     * 拉取当前最新所有配置
     */
    @RequestMapping(value = "/fetch")
    @ResponseBody
    public Object fetch(String appName,String env) {
    	FetchDto dto = new FetchDto();
    	DcConfigVersion entity = new DcConfigVersion();
    	entity.setAppId(dcAppService.getAppIdByName(appName));
    	if (entity.getAppId() == null) {
    		return dto;
    	}
    	entity.setEnvId(dcEnvService.getEnvIdByName(env));
    	if (entity.getEnvId() == null) {
    		return dto;
    	}
    	entity.setVersion(dcConfigVersionService.maxVersion(entity.getAppId(), entity.getEnvId()));
    	if (entity.getVersion() == null) {
    		return dto;
    	}
    	dto.setVersion(entity.getVersion());
    	Wrapper<DcConfigVersion> wrapper = new EntityWrapper<DcConfigVersion>(entity);
    	wrapper.ne("change_type", ChangeType.DEL.getCode());
    	List<DcConfigVersion> list = dcConfigVersionService.selectList(wrapper);
    	List<ConfigItem> configItems = new ArrayList<>();
    	if (!CollectionUtils.isEmpty(list)) {
    		list.forEach(action ->{
    			ConfigItem configItem = new ConfigItem();
    			configItem.setDesc(action.getConfigDesc());
    			configItem.setKey(action.getConfigKey());
    			configItem.setValue(action.getConfigValue());
    			
    			configItems.add(configItem);
    		});
    	}
    	dto.setItems(configItems);
    	return dto;
    }
    
    /**
     * 拉取更新配置(后一个版本)
     */
    @RequestMapping(value = "/pull")
    @ResponseBody
    public Object pull(String appName,String env,Long version) {
    	String key = String.format("%s_%s_%s", appName,env,version);
    	Object object = redisTemplate.opsForValue().get(key);
    	if (object != null) {
    		return object;
    	}
    	PullDto dto = new PullDto();
    	DcConfigVersion entity = new DcConfigVersion();
    	entity.setAppId(dcAppService.getAppIdByName(appName));
    	entity.setEnvId(dcEnvService.getEnvIdByName(env));
    	Long minVersion = dcConfigVersionService.minVersion(entity.getAppId(), entity.getEnvId(), version);
    	if (minVersion == null) {
    		redisTemplate.opsForValue().set(key, dto);
    		return dto;
    	}
    	entity.setVersion(minVersion);
    	Wrapper<DcConfigVersion> wrapper = new EntityWrapper<DcConfigVersion>(entity);
    	List<DcConfigVersion> list = dcConfigVersionService.selectList(wrapper);
    	dto.setVersion(minVersion);
    	if (!CollectionUtils.isEmpty(list)) {
    		list.forEach(action->{
    			ConfigItem item = new ConfigItem();
    			item.setDesc(action.getConfigDesc());
    			item.setKey(action.getConfigKey());
    			item.setValue(action.getConfigValue());
    			if (ChangeType.ADD.getCode().equals(action.getChangeType())) {
    				dto.getAddItems().add(item);
    			}else if(ChangeType.UPDATE.getCode().equals(action.getChangeType())) {
    				dto.getUpdateItems().add(item);
    			}else if(ChangeType.DEL.getCode().equals(action.getChangeType())) {
    				dto.getDelItems().add(item);
    			}
    		});
    	}
    	redisTemplate.opsForValue().set(key, dto);
    	return dto;
    }
}
